﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using TrayMes;

namespace TrayCheckControl
{
    class ClientState : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (null != PropertyChanged)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private SocketState state = SocketState.Uninitialized;
        private string description = "未创建";
        private System.Drawing.Image image = ClientState.Images[0];
        private static readonly System.Drawing.Image[] Images = new System.Drawing.Image[4] {
            Properties.Resources.ledOff,
            Properties.Resources.ledOff,
            Properties.Resources.ledOn,
            Properties.Resources.ledOn};
        public SocketState State
        {
            get => this.state;
            set
            {
                this.state = value;
                var attrs = this.state.GetType().GetField(this.state.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (null != attrs && attrs.Length > 0)
                {
                    SocketStateAttribute attr = attrs[0] as SocketStateAttribute;
                    this.Description = null != attr?.Message ? attr.Message : string.Empty;
                }
                int index = (int)state;
                if (index >= ClientState.Images.Length)
                {
                    index = 0;
                }
                this.Image = ClientState.Images[index];
                this.OnPropertyChanged("State");
            }
        }
        public string Description
        {
            get => this.description;
            private set
            {
                this.description = value;
                this.OnPropertyChanged("Description");
            }
        }
        public System.Drawing.Image Image
        {
            get => this.image;
            private set
            {
                this.image = value;
                this.OnPropertyChanged("Image");
            }
        }
    }
}
